package com.alumni_circle.core.web;


import com.alumni_circle.core.constant.ResponseCode;
import com.alumni_circle.core.entity.Picture;
import com.alumni_circle.core.entity.ResponseListModel;
import com.alumni_circle.core.entity.ResponseModel;
import com.alumni_circle.core.service.IPictureService;
import com.alumni_circle.core.utils.Modify;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author 龙小虬
 */
public abstract class BaseController<T> {

    /**
     * 返回成功
     * @return
     */
    public ResponseModel<T> successMsg() {

        return new ResponseModel<T>();
    }

    /**
     * 返回成功，自定义状态码
     * @return
     */
    public ResponseModel<T> successMsg(String msg, T data) {

        return new ResponseModel<T>(data, ResponseCode.SUCCESS.getCode(), msg);
    }

    /**
     * 返回成功，自定义状态码
     * @return
     */
    public ResponseModel<T> successMsg(T data) {

        return new ResponseModel<T>(data);
    }


    /**
     * 返回成功，自定义状态码
     * @return
     */
    public ResponseModel<T> failMsg(int code, String msg, T data) {

        return new ResponseModel<T>(data, code, msg);
    }



    /**
     * 返回成功，自定义状态码
     * @return
     */
    public ResponseListModel<T> successMsg(String msg, List<T> data) {

        return new ResponseListModel<T>(data, ResponseCode.SUCCESS.getCode(), msg);
    }

    /**
     * 返回成功，自定义状态码
     * @return
     */
    public ResponseListModel<T> successMsg(List<T> data) {

        return new ResponseListModel<T>(data);
    }

    /**
     * 返回成功，自定义状态码
     * @return
     */
    public ResponseListModel<T> failMsg(int code, String msg) {

        return new ResponseListModel<T>(code, msg);
    }

    /**
     * 返回成功，自定义状态码
     * @return
     */
    public ResponseListModel<T> failMsg(int code, String msg, List<T> data) {

        return new ResponseListModel<T>(data, code, msg);
    }


    @Resource
    private IPictureService pictureService;

    /**
     *
     * @param type 文章类型（0.需要展示第一张图片，1.不需要展示第一张图片）
     * @param articleId 文章id
     * @param path 图片路径
     */
    protected void picturePath(Integer type, String articleId, String path){

        //保存
        if( type==0 ) {
            path = Modify.modifyImageLast(path);
            if(path != null){
                pictureService.save(new Picture(articleId, path));
            }
        }else if(type ==1){
            path = Modify.modifyImage(path);
            if(path!=null){
                pictureService.save(new Picture(articleId,path));
            }
        }
    }


    protected void picturePathDelete(String id){
        QueryWrapper<Picture> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("article_id",id);
        pictureService.remove(queryWrapper);
    }


}

